import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class SocketThreadForChat extends Thread {
	
	String message;
	String hostName;
	String UserName;
	int portNumber;
	Socket connectionToReceiver;
	
	public SocketThreadForChat(String msg, String hostName, int portNumber, String UserName) {
		this.message = msg;
		this.hostName = hostName;
		this.portNumber = portNumber;
		this.UserName = UserName;
	}
	
	public void run() {
		try {
			connectionToReceiver = new Socket(hostName, portNumber);			
			if( connectionToReceiver == null ) return;
			System.out.println("The socket is bound:  " + connectionToReceiver.isBound());
			System.out.println("This is the port on the other side:  " + connectionToReceiver.getPort());
			System.out.println("The message on this side is : " + message);
			
			DataOutputStream Writer = new DataOutputStream(connectionToReceiver.getOutputStream());
			
			Writer.writeInt(1);
			Writer.writeUTF(UserName.concat(" ").concat(message));
			Writer.flush();
			
			//PrintWriter writer = new PrintWriter(connectionToReceiver.getOutputStream());
			//writer.print(UserName);
			//writer.print(message);
			
			//Writer.flush();
			connectionToReceiver.close();
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println("Connection open");
	}
}